package com.atguigu.scw.manager.controller.permission;

import com.atguigu.scw.manager.bean.TPermission;
import com.atguigu.scw.manager.bean.TRole;
import com.atguigu.scw.manager.service.PermissionService;
import com.atguigu.scw.manager.service.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;

@RequestMapping("/permission/role")
@Controller
public class RoleController {
    private final Logger logger = LoggerFactory.getLogger(RoleController.class);

    @Autowired
    RoleService roleService;
    @Autowired
    PermissionService permissionService;


    @RequestMapping("/list")
    public String roleList(@RequestParam(value = "pageSize", defaultValue = "2") Integer pageSize,
                           @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                           Model model) {
        PageHelper.startPage(pageNum, pageSize);
        List<TRole> allRole = roleService.getAllRole();
        PageInfo<TRole> rolePageInfo = new PageInfo<>(allRole, 5);
        model.addAttribute("rolePage", rolePageInfo);
        return "manager/permission/role";
    }

    /**
     * 返回所有权限的json数据
     *
     * @return 权限json数据
     */
    @ResponseBody
    @RequestMapping("/getAllPermission")
    public List<TPermission> getAllPermission() {
        return permissionService.getPermissions();
    }

    /**
     * 查询角色对应的权限
     *
     * @param roleId 角色ID
     * @return 权限列表
     */
    @ResponseBody
    @RequestMapping("/permission/{roleId}")
    public List<TPermission> getRolePermission(@PathVariable("roleId") Integer roleId) {
        return roleService.getRolePermission(roleId);
    }

    @ResponseBody
    @RequestMapping("/update")
    public String updateRolePermission(@RequestParam("roleId") Integer roleId,
                                       @RequestParam("permissionIds") String permissionIds) {
        boolean flag = roleService.updateRolePermission(roleId, permissionIds);
        return "ok";
    }

    /**
     * 角色维护2页面使用ajax展示数据，所以这里只是跳转页面
     *
     * @return
     */
    @RequestMapping("/list2")
    public String list2() {
        return "manager/permission/role2";
    }

    /**
     * 使用@ResponseBody 表示启用消息转换器 HttpMessageConverter
     * 如果返回结果为对象(Entity Class, List, Map)类型：启用这个转换器->MappingJackson2HttpMessageConverter 将对象序列化为jason串，使用Jackson组件转换
     * 如果返回结果为String类型：启用这个转换器->StringHttpMessageConverter 将字符串原样输出
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ResponseBody
    @RequestMapping("/loadData")
    public PageInfo<TRole> loadData(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                    @RequestParam(value = "pageSize", required = false, defaultValue = "2") Integer pageSize,
                                    @RequestParam(value = "searchParam", required = false, defaultValue = "") String searchParam) {
        logger.debug("pageNum={}, pageSize={}, searchParam={}", pageNum, pageSize, searchParam);
        PageHelper.startPage(pageNum, pageSize);
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("condition", searchParam);
        List<TRole> allRole = roleService.listRolePage(paramMap);
        return new PageInfo<>(allRole, 5);
    }

    @ResponseBody
    @RequestMapping("/addRole")
    public String addRole(TRole role) {
        roleService.saveRole(role);
        return "ok";
    }

    @ResponseBody
    @RequestMapping("/getRoleById")
    public TRole getRoleById(Integer roleId) {
        return roleService.getRoleById(roleId);
    }

    @ResponseBody
    @RequestMapping("/updateRole")
    public String updateRole(TRole role){
        roleService.updateRole(role);
        return "ok";
    }

    @ResponseBody
    @RequestMapping("deleteRole")
    public String deleteRole(Integer roleId) {
        roleService.deleteRole(roleId);
        return "ok";
    }
}
